package thinkingJava.chapter16;

import java.util.Arrays;
import java.util.Random;

import thinkingJava.chapter15.Generator;

public class CompType implements Comparable<CompType> {
	int i,j;
	private static int count = 1;
	public CompType(int n1, int n2) {
		i = n1;
		j = n2;
	}
	public String toString() {
		String result = "[ i = " + i + " , j = " + j + "]";
		if (count++ % 3 == 0) {
			result += "\n";
		}
		return result;
	}
	public int compareTo(CompType o) {
		return i < o.i ? -1 : (i == o.i ? 0 : 1);
	}
	private static Random rand = new Random(47);
	public static Generator<CompType> generator() {
		return new Generator<CompType>(){
			public CompType next() {
				return new CompType(rand.nextInt(100),rand.nextInt(100));
			}
		};
	}
	public static void main(String[] args) {
//		CompType[] a = Generated.array(new CompType[12], generator());
		CompType[] a = new CompType[12];
		for (int i = 0; i < a.length; i++) {
			a[i] = generator().next();
		}
		System.out.println("before sorted: ");
		System.out.println(Arrays.toString(a));
		Arrays.sort(a);//Comparable
		System.out.println("after sorted: ");
		System.out.println(Arrays.toString(a));
	}
}
